//
//  MessageListViewController.m
//  homeland
//
//  Created by SunShine on 13-11-5.
//  Copyright (c) 2013年 xzysun. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "MyMessageItem.h"
#import "MessageListViewController.h"
#import "ResidentComplainRecordController.h"
#import "SupervisionDetailController.h"
#import "AdvisoryDetailInfoViewController.h"
#import "NoticesItem.h"
#import "MyConcernNoticesDetailViewController.h"
#import "MessageListService.h"

@interface MessageListViewController ()
@property (strong, nonatomic) MessageService *dataService;
@property (strong, nonatomic) NSArray *dataList;
@end

@implementation MessageListViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = _listItem.flmc;
    //self.dataService = [MessageService new];
    //_dataService.delegate = self;
    //请求服务器 PERSONAL_ALL_MESSAGE_LIST_URL  list
    [[MessageListService new]queryMyMessageList:^(NSArray *data) {
        self.dataList = data;
    } Failure:^(NSString *msg) {
        
    }];
   // [_dataService loadMessageListWithFldm:_listItem.fldm];
   //_dataService.noticeDelegate = self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Table Method
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataService.dataList?_dataService.dataList.count:0;
}
//初始化数据
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"MessageListCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    //[dic objectForKey:@"cticketname"];
   //NSDictionary *dic = [self.dataList objectAtIndex:indexPath.row];
    MyMessageItem *item = [self.dataList objectAtIndex:indexPath.row];
    cell.textLabel.text = item.dataTitle;
//    if ([_listItem.fldm isEqualToString:@"tzgg"]) {
//        TZGGListItem *item = [_dataService.dataList objectAtIndex:indexPath.row];
//        cell.textLabel.text = item.noticeTitle;
//    } else if ([_listItem.fldm isEqualToString:@"wyjd"]) {
// 
//        WYJDListItem *item = [_dataService.dataList objectAtIndex:indexPath.row];
//        cell.textLabel.text = item.title;
//    } else if ([_listItem.fldm isEqualToString:@"qzxx"]) {
//        QZXXListItem *item = [_dataService.dataList objectAtIndex:indexPath.row];
//        cell.textLabel.text = item.content;
//    } else if ([_listItem.fldm isEqualToString:@"zczx"]) {
//        ZCZXListItem *item = [_dataService.dataList objectAtIndex:indexPath.row];
//        cell.textLabel.text = item.dealCode;
//    } else if ([_listItem.fldm isEqualToString:@"zwzxwsyy"]) {
// 
//    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

//事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if ([_listItem.fldm isEqualToString:@"tzgg"]) {
        TZGGListItem *item = [_dataService.dataList objectAtIndex:indexPath.row];
        [self startLoadingViewWithText:@"正在载入..."];
        [_dataService loadNoticeForID:item.noticeId];
        //标记已读
        [_dataService markMessageReadForType:@"tzgg" AndID:item.noticeId];
    } else if ([_listItem.fldm isEqualToString:@"wyjd"]) {
        WYJDListItem *item = [_dataService.dataList objectAtIndex:indexPath.row];
        SupervisionDetailController *vc = [[SupervisionDetailController alloc]initWithNibName:@"SupervisionDetailController" bundle:nil];
        ComplainEntity *dataItem = [ComplainEntity new];
        dataItem.title = item.title;
        dataItem.serviceOrgName = item.serviceOrgName;
        dataItem.dealStatus = item.dealStatus;
        dataItem.dealContent = item.dealContent;
        dataItem.dealCode = @"";
        dataItem.userName = item.userName;
        dataItem.dealTime = item.dealTime;
        dataItem.complainContent = item.complainContent;
        vc.config = [NSMutableDictionary dictionaryWithObjectsAndKeys:dataItem, @"dataItem", @"我要监督", @"key", nil];
        [self.navigationController pushViewController:vc animated:YES];
        //标记已读
        [_dataService markMessageReadForType:@"wyjd" AndID:item.Id];
    } else if ([_listItem.fldm isEqualToString:@"qzxx"]) {
        QZXXListItem *item = [_dataService.dataList objectAtIndex:indexPath.row];
        ResidentComplainRecordController *vc = [[ResidentComplainRecordController alloc]initWithNibName:@"ResidentComplainRecordController" bundle:nil];
        ResidentDataItem *dataItem = [ResidentDataItem new];
        dataItem.subject = item.subject;
        dataItem.slid = item.slid;
        dataItem.replyStatus = item.treatType;
        dataItem.replyContent = item.replyOpinion;
        dataItem.content = item.content;
        dataItem.createDate = item.createDate;
        dataItem.transUnitName = item.transUnitName;
        vc.item = dataItem;
        [self.navigationController pushViewController:vc animated:YES];
        //标记已读
        [_dataService markMessageReadForType:@"qzxx" AndID:item.Id];
    } else if ([_listItem.fldm isEqualToString:@"zczx"]) {
        ZCZXListItem *item = [_dataService.dataList objectAtIndex:indexPath.row];
        AdvisoryDetailInfoViewController *vc = [[AdvisoryDetailInfoViewController alloc]initWithNibName:@"AdvisoryDetailInfoViewController" bundle:nil];
        vc.config = [NSMutableDictionary dictionaryWithObjectsAndKeys:item.Id, @"id",  nil];
        [self.navigationController pushViewController:vc animated:YES];
        //标记已读
        [_dataService markMessageReadForType:@"zczx" AndID:item.Id];
    } else if ([_listItem.fldm isEqualToString:@"zwzxwsyy"]) {
        //        [self queryMesageWithURL:PERSONAL_MESSAGE_WSYY_LIST Params:params DataItemName:@"WSYYListItem"];
        //标记已读
//        [_dataService markMessageReadForType:@"zwzxwsyy" AndID:item.Id];
    }
}

//#pragma mark - Data Service
//-(void)loadDataFinished
//{
//    [self stopLoadingView];
//    [_tableView reloadData];
//}
//
//-(void)loadDataFaild:(NSString *)msg
//{
//    [self stopLoadingView];
//    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//    [alert show];
//}
//- Notice Delegate
//#pragma mark
//-(void)loadNoticeSuccess:(id)data
//{
//    [self stopLoadingView];
//    TZGGDetailItem *detailItem = (TZGGDetailItem *)data;
//    NoticesItem *item = [NoticesItem new];
//    item.title = detailItem.title;
//    item.detail = detailItem.detail;
//    item.fileUrl = detailItem.fileUrl;
//    MyConcernNoticesDetailViewController *detailVC = [[MyConcernNoticesDetailViewController alloc]initWithNibName:@"MyConcernNoticesDetailViewController" bundle:nil];
//    detailVC.config = [NSDictionary dictionaryWithObjectsAndKeys:item, @"dataItem", nil];
//    [self.navigationController pushViewController:detailVC animated:YES];
//}
//
//-(void)loadNoticeFail:(NSString *)msg
//{
//    [self stopLoadingView];
//    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//    [alert show];
//}
@end
